#!/usr/bin/awk
BEGIN{
	FS = ";" 
	print "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
	print "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xml:base=\"http://purl.org/weso/cpv/2008\" xmlns:cpv=\"http://http://purl.org/weso/cpv/2008#\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
	generateTopConcepts()
}
{ 
	
	#If 1st row
	if (NR == 1){		
   		for(i = 2 ; i <= NF ; i++){
			gsub(/"/, "", $i)
			languages[i] = $i	
		} 
	}else{
   		gsub(/"/, "", $1)
		print "<cpv:cpvConcept rdf:about=\""formatId($1)"\">"
		print "<rdf:type>"getType($1)"</rdf:type>"
		print "<dc:identifier>"$1"</dc:identifier>"
		print "<skos:broader>"getBroader(id)"</skos:broader>"
		#"CODE","BG","CS","DA","DE","EL","EN","ES","ET","FI","FR","GA","HU","IT","LT","LV","MT","NL","PL","PT","RO","SK","SL","SV"
		for(i = 2 ; i <= NF ; i++){
			gsub(/"/, "", $i)			
			print "<skos:prefLabel xml:lang=\""languages[i]"\">"$i"</skos:prefLabel>"
		}
	
		print "</cpv:cpvConcept>"
	}

}
END{ 
	print "</rdf:RDF>"
}

#1- f(PSC)=Top classes Hierarchy

function generateTopConcepts(){

print "<cpv:cpvConcept xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" rdf:type=\"skos:Concept\"/>"
print ""
print "<cpv:cpvConcept rdf:about=\"http://purl.org/weso/cpv/2008#cpv-division\">"
print "	<skos:prefLabel xml:lang=\"en\">Division</skos:prefLabel>"
print "	<skos:example>01000000-7</skos:example>"
print "</cpv:cpvConcept>"

print "<cpv:cpvConcept rdf:about=\"http://purl.org/weso/cpv/2008#cpv-group\">"
print "	<skos:prefLabel xml:lang=\"en\">Group</skos:prefLabel>"
print "	<skos:example>01100000-8</skos:example>"
print "</cpv:cpvConcept>"


print "<cpv:cpvConcept rdf:about=\"http://purl.org/weso/cpv/2008#cpv-class\">"
print "	<skos:prefLabel xml:lang=\"en\">Class</skos:prefLabel>"
print "	<skos:example>01110000-1</skos:example>"
print "</cpv:cpvConcept>"

print "<cpv:cpvConcept rdf:about=\"http://purl.org/weso/cpv/2008#cpv-category\">"
print "	<skos:prefLabel xml:lang=\"en\">Category</skos:prefLabel>"
print "	<skos:example>01111000-8</skos:example>"
print "</cpv:cpvConcept>"

}

#2   f(Id)=Skos-Uri
function formatId(id){
   return "http://purl.org/weso/cpv/2008#"id 
}


#3   f(Id)=Skos-Broader-Classes -->
function getBroader(id){
  if (substr(id, 8, 1)=="0"){
   	return generateBroader(id,1,6,"0")
  }
  if (substr(id, 7, 2)=="00"){
   	return generateBroader(id,1,5,"00")
  }
  if (substr(id, 6, 3)=="000"){
   	return generateBroader(id,1,4,"000")
  }
  if (substr(id, 5, 4)=="0000"){
   	return generateBroader(id,1,3,"0000")
  }
  if (substr(id, 4, 5)=="00000"){
	return generateBroader(id,1,2,"00000")
  }
  if (substr(id, 3, 6)=="000000"){
	return "http://purl.org/weso/cpv/2008#cpv-class"
  }
  if (substr(id, 2, 7)=="0000000"){
	
  }
  if (substr(id, 1, 8)=="0000000"){
	
  }
}

function generate-broader(id,start,end,suffix){
	prefixId = substr(id,start,end) suffix
	#broaderId = search(prefixId)
	return formatId(broaderId)
}

   

#4   f(Id)=RDF-type -->
function getType(id){
  
  if (substr(id, 3, 6)=="000000"){		
 	return "http://purl.org/weso/cpv/2008#cpv-category"
  }
  if (substr(id, 4, 5)=="00000"){
   	return "http://purl.org/weso/cpv/2008#cpv-group"
  }
  if (substr(id, 5, 4)=="0000"){
	return "http://purl.org/weso/cpv/2008#cpv-class"
  }

 return "http://purl.org/weso/cpv/2008#cpv-category"

}







